package backTracking;

import java.util.ArrayList;
import java.util.List;

//17. 电话号码的字母组合
public class Num17_letterCombinations {
    List<String>list = new ArrayList<>();//结果集
    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0)return list;
        String[]words = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};//初始化字母表
        backTracking(digits , words , 0);
        return list;
    }
    StringBuilder sb = new StringBuilder();
    public void backTracking(String digits , String[]words , int num){
        if (num == digits.length()){
            list.add(sb.toString());
            return;
        }
        String str = words[digits.charAt(num) - '0'];//获取当前数字对应的字母组合
        for (int i = 0; i < str.length(); i++) {
            sb.append(str.charAt(i));
            backTracking(digits , words ,num + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }

}
